package net.mihome.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import net.mihome.app.AppContext;
import net.mihome.app.AppException;
import net.mihome.app.R;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.widget.LoadingDialog;

public class RestPassWordPhoneActivity extends BaseActivity {
	
	public final static int SEND_MSG=1;
	public final static int SEND_MSG_EXCEPT=-1;
	public final static int AFTER_SEND_MSG=2;
	public final static int CODE_ERROR=3;
	public final static int CODE_RIGHT=5;
	public final static int OK=4;
	public static String key;
	public static int recLen=60;
	//
	private Button sendButton;
	private Button okButton;
	private EditText phonEditText;
	private EditText codeEditText;
	private TextView errorTextView;
	private LoadingDialog loading;
	Handler handler;
	String userName;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initView();
	}
	private void initView(){
		setContentView(R.layout.find_password_phone);
		sendButton=(Button) findViewById(R.id.register_phone_back);
		okButton=(Button) findViewById(R.id.register_phone_btn_ok);
		phonEditText=(EditText) findViewById(R.id.register_phone_num_edit);
		codeEditText=(EditText) findViewById(R.id.register_phone_num_code);
		errorTextView=(TextView) findViewById(R.id.register_phone_msg);
		handler=new Handler(new Handler.Callback() {
			@Override
			public boolean handleMessage(Message arg0) {
				// TODO Auto-generated method stub
				if (loading != null)
					loading.dismiss();

//				Intent intent=getIntent().setClass(RestPassWordPhoneActivity.this,ToResetPasswordActivity.class);
//				intent.putExtra("phoneNum",phonEditText.getText().toString());
//				startActivity(intent);
//				RestPassWordPhoneActivity.this.finish();
				switch (arg0.what) {
				case AFTER_SEND_MSG:{
					errorTextView.setText(R.string.register_phone_btn_sended);
					sendButton.setText(R.string.register_phone_btn_resend);
					break;
				}
				case SEND_MSG_EXCEPT:{
                    Exception e= (Exception) arg0.obj;
                    if(e!=null&&e.getMessage()!=null){
                        errorTextView.setText(e.getMessage());
                    }else{
                        errorTextView.setText(R.string.register_phone_tv_network_error);
                    }
					sendButton.setText(R.string.register_phone_btn_send);
					break;
				}
				case CODE_ERROR:{
					errorTextView.setText(R.string.register_phone_tv_code_error);
					break;
				}
				case CODE_RIGHT:{
					Intent intent=getIntent().setClass(RestPassWordPhoneActivity.this,ToResetPasswordActivity.class);
					intent.putExtra("userName",userName);
					startActivity(intent);
					RestPassWordPhoneActivity.this.finish();
					break;
				}
				default:
					break;
				}
				return false;
			}
		});
		sendButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String phoneNumString=phonEditText.getText().toString();
				if(phoneNumString==null&&phoneNumString.trim().equals("")){
					errorTextView.setText(R.string.register_phone_tv_notnull);
				}else{
					sendMsgThread(phoneNumString);
					sendButton.setEnabled(false);
					timeDelay();
				}
			}
		});
		okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String code=codeEditText.getText().toString();
				String phoneNum=phonEditText.getText().toString();
				if(phoneNum==null||phoneNum.trim().equals("")){
					errorTextView.setText(R.string.register_phone_tv_phonenum_notnull);
				}else if(code==null||code.trim().equals("")){
					errorTextView.setText(R.string.register_phone_tv_code_notnull);
				}else{
					checkMsgThread(codeEditText.getText().toString(),phoneNum);
				}
			}
		});
	}
	/**
	 * 线程发送短信验证码
	 * */
	private void sendMsgThread(final String phoneNum) {
		loading = new LoadingDialog(this);
		loading.show();
		new Thread() {
			public void run() {
				Message msg = new Message();
				try {
					ResponsePojo<String> responsePojo = ((AppContext) getApplication()).sendMsgCodeExists(phoneNum);
                    if(responsePojo.getStatus()<0){
                        throw  new Exception(responsePojo.getMsg());
                    }
                    key=responsePojo.getMsg();
					msg.what = AFTER_SEND_MSG;
					msg.obj = key;
				} catch (Throwable e) {
					e.printStackTrace();
					msg.what = SEND_MSG_EXCEPT;
					msg.obj = e;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}
	/**
	 * 线程验证短信验证码
	 * */
	private void checkMsgThread(final String num,final String phoneNum) {
		loading = new LoadingDialog(this);
		loading.show();
		new Thread() {
			public void run() {
				Message msg = new Message();
				try {
                    userName=phoneNum;
					ResponsePojo responsePojo = ((AppContext) getApplication()).checkCode(phoneNum,num);
					if(responsePojo.getStatus()<0){
						msg.what = CODE_ERROR;
						msg.obj = key;
					}else{
						msg.what = CODE_RIGHT;
						msg.obj = key;
					}
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = SEND_MSG_EXCEPT;
					msg.obj = e;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}
	public void timeDelay(){
		recLen=60;
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				recLen--;
				sendButton.setText("" + recLen);
				if(recLen>0){
					handler.postDelayed(this, 1000);
				}else{
					sendButton.setEnabled(true);
					sendButton.setText(R.string.register_phone_btn_sended);
				}
			}
		},1000);
	}
}
